using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

using Generic.Algorithms.Sorting;
using NUnit.Framework;

namespace Generics.Net.Tests
{
    [TestFixture]
    public class NonGenericListSotTester
    {
        ISortStrategy<int> _strategy;
        public NonGenericListSotTester()
        {
            SetUp();
        }

        [TestFixtureSetUp]
        public void SetUp()
        {
            _strategy = new BubbleSortStrategy<int>();
        }

        [Test]
        public void ArrayListSort()
        {
            ArrayList list = new ArrayList();
            Random rand = new Random();
            for(int i=0; i<100; i++)
            {
                list.Add(rand.Next(0,100));
            }
            ListSorter<int> listSorter = new ListSorter<int>(list, _strategy);
            Assert.AreEqual(IsListSorted(listSorter.Sort(), SortDirection.Ascending), true);
            Assert.AreEqual(IsListSorted(listSorter.Sort(SortDirection.Descending), SortDirection.Descending), true);
            
        }

        [Test]
        public void ArraySort()
        {
            int[] list = new int[100];
            Random rand = new Random();
            for (int i = 0; i < 100; i++)
            {
                list[i] = rand.Next(0, 100);
            }

            ListSorter<int> listSorter = new ListSorter<int>(list, _strategy);
            Assert.AreEqual(IsListSorted(listSorter.Sort(), SortDirection.Ascending), true);
            Assert.AreEqual(IsListSorted(listSorter.Sort(SortDirection.Descending), SortDirection.Descending), true);

        }

        bool IsListSorted(IList<int> list, SortDirection direction)
        {
            if (direction == SortDirection.Ascending)
            {
                int prevValue = -999;
                for (int i = 0; i < list.Count; i++)
                {
                    if (prevValue > (int)list[i])
                        return false;
                    prevValue = (int)list[i];
                }
                return true;
            }
            else
            {
                int prevValue = 999;
                for (int i = 0; i < list.Count; i++)
                {
                    if (prevValue < (int)list[i])
                        return false;
                    prevValue = (int)list[i];
                }
                return true;
            }
        }

    }
}
